package com.demo.accessibilitykeeper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * 开机自启动广播接收器
 * 功能：监听开机完成事件，自动启动黑屏遮罩服务
 */
public class BootReceiver extends BroadcastReceiver {
    private static final String TAG = "BootReceiver";
    private static final String PREFS_NAME = "auto_mask_prefs";
    private static final String KEY_AUTO_MASK_ENABLED = "auto_mask_enabled";
    
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        
        if (Intent.ACTION_BOOT_COMPLETED.equals(action) || 
            Intent.ACTION_MY_PACKAGE_REPLACED.equals(action) ||
            Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
            
            Log.d(TAG, "🚀 BOOT_COMPLETED_RECEIVED - Starting auto mask service");
            
            // 检查自动黑屏是否已启用
            if (isAutoMaskEnabled(context)) {
                Log.d(TAG, "✅ AUTO_MASK_ENABLED - Starting AutoMaskService");
                
                // 启动自动黑屏服务
                Intent autoMaskIntent = new Intent(context, AutoMaskService.class);
                autoMaskIntent.putExtra("action", "start_mask_immediately");
                context.startService(autoMaskIntent);
                
                Log.d(TAG, "🎭 AUTO_MASK_SERVICE_STARTED_SUCCESSFULLY");
            } else {
                Log.d(TAG, "❌ AUTO_MASK_DISABLED - Skipping auto mask start");
            }
        }
    }
    
    /**
     * 检查自动黑屏是否已启用
     */
    private boolean isAutoMaskEnabled(Context context) {
        try {
            SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            return prefs.getBoolean(KEY_AUTO_MASK_ENABLED, false);
        } catch (Exception e) {
            Log.e(TAG, "❌ ERROR_CHECKING_AUTO_MASK_STATUS", e);
            return false;
        }
    }
}